package com.weipai.springboot.module.util;
import com.weipai.springboot.model.api.Tree;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Guyatong
 * @Date: 2018/06/29 19:18
 */
public class TreeUtil {

    public static Map<String,Object> mapArray = new LinkedHashMap<String, Object>();
    
    public List<Tree> menuCommon;
    public List<Object> list = new ArrayList<Object>();

    public List<Object> menuList(List<Tree> menu){
        this.menuCommon = menu;
        for (Tree x : menu) {
            if(x.getCategoryRank().equals("顶级") && x.getStatus()==0){
                Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
                mapArr.put("id", x.getId());
                mapArr.put("name", x.getName());
                mapArr.put("pid", x.getpId());
                mapArr.put("img", x.getImg());
                mapArr.put("sort", x.getSort());
                mapArr.put("children", menuChild(x.getId()));
                x.setStatus(-1);
                list.add(mapArr);
                continue;
            }
        }
        return list;
    }

    public List<Object> menuChild(String id){
        List<Object> lists = new ArrayList<Object>();
        for(Tree a:menuCommon){
            Map<String,Object> childArray = new LinkedHashMap<String, Object>();
            if(a.getpId().equals(id) && a.getStatus()==0){
                childArray.put("id", a.getId());
                childArray.put("name", a.getName());
                childArray.put("pid", a.getpId());
                childArray.put("img", a.getImg());
                childArray.put("sort", a.getSort());
                childArray.put("children", menuChild(a.getId()));
                a.setStatus(-1);
                lists.add(childArray);
            }
        }
        return lists;
    }
    
}
